package com.com.example.Json;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.examplee.username.httpfrist_01.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonActivity extends AppCompatActivity {
String str="{\n" +
        "  \"name\":\"张三\",\n" +
        "\n" +
        "  \"data\":{\n" +
        "    \"address\":\"重庆\",\n" +
        "    \"school\":\"重庆三中\"\n" +
        "\n" +
        "  },\n" +
        "  \"body\":[{\"id\":1,\"eamil\":\"2345672@aa\",\"albul\":1345}],\n" +
        "  \"No\":234567\n" +
        "\n" +
        "}";

    TextView tv_object,tv_data,tv_number;
    String str_object;
    String str_data;
    int int_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);
        tv_object= (TextView) findViewById(R.id.tv_object);
        tv_data= (TextView) findViewById(R.id.tv_data);
        tv_number= (TextView) findViewById(R.id.tv_number);

        try {

            //            获取第一个层的
            JSONObject jsonObject=new JSONObject(str);
            str_data=jsonObject.getString("name");
            tv_object.setText(str_data);
//            获取第二层里面的
            JSONObject js=new JSONObject(str);
            str_data=js.getJSONObject("data").getString("school");
            tv_data.setText(str_data);
//            获取数组里面的
            JSONObject jsob=new JSONObject(str);
            JSONArray jsonArray=jsob.getJSONArray("body");
            for (int i=0;i<jsonArray.length();i++){
                jsob=jsonArray.getJSONObject(i);

                int_data=jsob.getInt("id");
                tv_number.setText(int_data+"");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
